package com.controller;

import com.github.pagehelper.PageInfo;
import com.model.auto.CollectInfo;
import com.service.impl.CollectServiceImpl;
import com.util.Page;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 收藏模块
 *
 * @author zhaoyifan
 * @date 2023/9/2 15:26
 */
@RestController
@RequestMapping("/collect")
public class CollectController extends BaseController{

    @Autowired
    CollectServiceImpl collectService;

    /**
     * 我的收藏列表
     *
     * @param page   分页参数
     * @param userId 用户id
     * @param type   收藏类型1:学校2:专业
     * @param code   学校编码或者专业编码
     * @return
     */
    @GetMapping("/myList")
    public R myList(Page page, Long userId, Integer type, String code, HttpServletRequest request) {
        userId = getUserId(request);
        PageInfo<CollectInfo> pageInfo = collectService.myList(page, userId, type, code);
        return R.ok(pageInfo);
    }

    /**
     * 添加收藏
     * @param collectInfo
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody CollectInfo collectInfo) {
        return collectService.add(collectInfo);
    }

    /**
     * 是否收藏
     * @param collectInfo
     * @return
     */
    @PostMapping("/isCollect")
    public R isCollect(@RequestBody CollectInfo collectInfo) {
        Boolean isCollect = collectService.isCollect(collectInfo);
        return R.ok(isCollect);
    }

    /**
     * 取消收藏
     * @param collectInfos
     * @return
     */
    @PostMapping("/cancel")
    public R cancel(@RequestBody List<CollectInfo> collectInfos, HttpServletRequest request) {
        Long userId = getUserId(request);
        if(ObjectUtils.isEmpty(userId)){
            return R.error("登录信息失效，请重新登录。");
        }
        return collectService.cancel(collectInfos, userId);
    }
}
